package org.hylexus.hadoop.hadoop_2_test.hdfs;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.hylexus.hadoop.hadoop_2_test.common.HDFSUtil;
import org.junit.Test;

public class HDFSTest {

	@Test
	public void testDownload() {
		try {
			FileSystem fs = HDFSUtil.getFS("");
			FSDataInputStream in = fs.open(new Path("/hello"));
			IOUtils.copyBytes(in, System.err, 4096, true);
			fs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testUpload() {
		try {
			FileInputStream in = new FileInputStream("./pom.xml");
			FileSystem fs = HDFSUtil.getFS();
			FSDataOutputStream out = fs.create(new Path("/pom.xml"));
			IOUtils.copyBytes(in, out, 4096, true);
			fs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testDelete() {
		FileSystem fs = HDFSUtil.getFS();
		try {
			boolean delete = fs.delete(new Path("/hadoop-2.6.2.tar.gz"), true);
			System.err.println(delete);
			fs.close();
		} catch (IllegalArgumentException e) {
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
